#ifndef MASTER_H
#define MASTER_H

#include <vector>
#include <string>
#include <pthread.h>
#include "libnet.h"
#include "Protocole.h"

struct obs
{
	SocketServer sock;
	std::string login;
	std::string roomname;
	pthread_t thread_obs;
};

struct etu
{
	SocketServer sock;
	std::string login;
	std::string roomname;
	std::string activity;
	pthread_t thread_etu;
	pthread_mutex_t writing;
	bool isAssistant;
};


struct ast
{
	SocketServer sock;
	std::string login;
	std::string etulogin;
	pthread_t thread_ast;
};

struct room
{
	std::string name;
	std::vector<std::string> activities;
};
		
class Master
{
	public:
	
		Master(int port = 9999);
		~Master();
		
		void start();
		
		static void *read_ast(void *arg);
		static void *manageAst(void *arg);
		
		static void *read_etu(void *arg);
		static void *manageEtu(void *arg);
		static void *manageObs(void *arg);
		
		static void *waitConnections(void *arg);
		static void *connect(void *arg);
		
	private:
		
		void sendAllObs(const std::string &msg, const std::string &roomname, const std::string &type);
		
		// Server
		bool checkLogin(const std::string &login);
		bool checkRoom(const std::string &name);
		
		// network
		int getProto(std::string &msg, Socket *sock);
		void sendProto(const std::string msg, Socket *sock);
		int getString(std::string &msg, int size, Socket *sock, int bufsize = 256);
		
		std::vector<obs> obss;
		std::vector<ast> asts;
		std::vector<etu> etus;
		std::vector<room> rooms;
		typedef std::vector<room>::iterator roomIter;
		typedef std::vector<obs>::iterator obsIter;
		typedef std::vector<etu>::iterator etuIter;
		typedef std::vector<ast>::iterator astIter;

		SocketServer server;
		Protocole proto;

};

#endif
